import axios from "axios";
import md5 from "js-md5";
// var Mint = require("mint-ui");
var _this = this;
var HeaderLogin = md5("ylbbapi&ylbbapilogin").toUpperCase();
const ajaxMethod = {
  ajax: function(type, method, param) {
    // Mint.Indicator.open();
    return axios({
      headers: {
        Authorization: "BasicAuth " + HeaderLogin
      },
      method: "get",
      url: process.env.API_ROOT + "/api/invoke/" + method,
      params: param
    })
      .then(response => {
        // Mint.Indicator.close();
        return response.data;
      })
      .catch(error => {
        errProcess(error);
        console.log("get请求失败:", error);
        // Mint.Indicator.close();
      });
  },
  ajaxPost: function(type, method, param, loadtxt) {
    var loadtxts = loadtxt || "";
    // Mint.Indicator.open(loadtxts);
    return axios({
      headers: {
        Authorization: "BasicAuth " + HeaderLogin
      },
      method: "post",
      url: process.env.API_ROOT + "/api/invoke/" + method,
      data: param
    })
      .then(response => {
        // Mint.Indicator.close();
        return response.data;
      })
      .catch(error => {
        errProcess(error);
        console.log("post请求失败:", error);
        // Mint.Indicator.close();
      });
  }
};
//输出接口请求方法
export default {
  get: function(method, param) {
    return ajaxMethod.ajax("get", method, param);
  },
  post: function(method, param, loadtxt) {
    return ajaxMethod.ajaxPost("post", method, param, loadtxt);
  }
};

function errProcess(error) {
  var errCode = error.response.status,
    apiName = error.response.config.url.replace("/api/invoke/", ""),
    printErr = "";
  switch (errCode) {
    case 403:
      printErr = "非法接口调用，拒绝访问";
      break;

    case 404:
      printErr = apiName + " 接口不存在";
      break;

    case 500:
      printErr = "内部服务器错误，请联系管理员";
      break;

    case 504:
      printErr = "网络错误，请检查是否已联网";
      break;

    default:
      printErr = "网络错误";
  }
  alert(printErr);
}
